package com.ctc.kmpdemo.android.pages.main

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.CornerRadius
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.lifecycleScope
import com.ctc.kmpdemo.Greeting
import com.ctc.kmpdemo.android.MyApplicationTheme
import com.ctc.kmpdemo.bean.ArticleBean
import com.ctc.kmpdemo.bean.HarmonyDataBean
import com.ctc.kmpdemo.net.ApiService
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {

    val harmonyData = mutableStateOf(HarmonyDataBean())

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
                        GreetingView(Greeting().greet())

                        val tools = harmonyData.value.tools
                        Text(
                            "常用工具",
                            fontSize = 18.sp,
                            color = Color.Black,
                            modifier = Modifier.padding(bottom = 10.dp)
                        )
                        tools?.articleList?.forEach { article ->
                            ArticleView(article)
                        }
                        val links = harmonyData.value.links
                        Text(
                            "常用链接",
                            fontSize = 18.sp,
                            color = Color.Black,
                            modifier = Modifier.padding(bottom = 10.dp)
                        )
                        links?.articleList?.forEach { article ->
                            ArticleView(article)
                        }
                        val open_sources = harmonyData.value.open_sources
                        Text(
                            "必备开源库",
                            fontSize = 18.sp,
                            color = Color.Black,
                            modifier = Modifier.padding(bottom = 10.dp)
                        )
                        open_sources?.articleList?.forEach { article ->
                            ArticleView(article)
                        }
                    }
                }
            }
        }

        getHarmonyColumnArticleList()
    }

    /**
     * 获取鸿蒙专栏文章列表
     */
    fun getHarmonyColumnArticleList() {
        lifecycleScope.launch {
            ApiService.getHarmonyColumnArticleList().data?.let { data ->
                harmonyData.value = data
            }
        }
    }

    @Composable
    fun ArticleView(bean: ArticleBean) {
        Column(
            modifier = Modifier
                .padding(horizontal = 15.dp, vertical = 5.dp)
                .background(color = Color.White, shape = RoundedCornerShape(5.dp))
                .padding(10.dp)
        ) {
            Text(text = bean.title, fontSize = 16.sp, color = Color.Black)
            Text(
                text = bean.desc,
                fontSize = 14.sp,
                color = Color.Gray,
                modifier = Modifier.padding(top = 5.dp)
            )
        }
    }
}

@Composable
fun GreetingView(text: String) {
    Text(text = text)
}

@Preview(showSystemUi = true)
@Composable
fun DefaultPreview() {
    MyApplicationTheme {
        GreetingView("Hello, Android!")
    }
}
